<template>
  <div>
    <el-dialog
      class="car-owner-add"
      :title="isEdit ? '信息修改' : '新增充电监测设备'"
      :visible.sync="dialogVisible"
      width="800px"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="主机名称" prop="alarmEquipmentName">
              <el-input v-model="ruleForm.alarmEquipmentName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机编码" prop="alarmEquipmentCode">
              <el-input v-model="ruleForm.alarmEquipmentCode"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="ruleForm.longitude"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="ruleForm.latitude"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备唯一ID" prop="equipmentId">
              <el-input v-model="ruleForm.equipmentId"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="text-align: right; padding-right: 60px">
            <el-button type="text" @click="getCoordinates"
              >经纬度采集</el-button
            >
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="详细位置" prop="address">
              <el-input v-model="ruleForm.address"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在小区" prop="orgId">
              <org-select v-model="ruleForm.orgId" @select="orgSelect" :show-all="false"></org-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="设备照片" prop="photo">
              <el-upload
                class="upload-demo"
                action="/xds/file/photo-upload"
                :on-success="uploadSuccess"
                :limit="5"
              >
                <el-button type="text" size="small">上传文件</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button @click="doReset" v-show="isResetShow">重 置</el-button>
        <el-button type="primary" @click="doConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <coordinates ref="coordinates"></coordinates>
  </div>
</template>


<script>
import * as API from "@/api/business/alarm-equipment";
import * as PhotoApi from "@/api/business/photo";
import coordinates from "@/components/coordinates/index";
export default {
  components: { coordinates },
  data() {
    return {
      dialogVisible: false,
      isEdit: false,
      isResetShow: false,
      filePath: null,
      ruleForm: {
        alarmEquipmentName: "",
        alarmEquipmentCode: "",
        longitude: "",
        latitude: "",
        equipmentId: "",
        orgId: "",
        orgName: "",
        address: "",
      },
      rules: {
        alarmEquipmentName: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        ],
        alarmEquipmentCode: [
          { required: true, message: "请输入设备编码", trigger: "blur" },
        ],
        equipmentId: [
          { required: true, message: "请输入设备唯一ID", trigger: "blur" },
        ],
        longitude: [{ required: true, message: "请选择经度", trigger: "blur" }],
        latitude: [{ required: true, message: "请输入纬度", trigger: "blur" }],
        address: [
          {
            required: true,
            message: "请输入设备所在位置详细信息",
            trigger: "blur",
          },
        ],
        orgId: [{ required: true, message: "请选择小区", trigger: "blur" }],
      }
    };
  },
  methods: {
    show() {
      this.dialogVisible = true;
      this.isResetShow = true
    },
    edit(ruleForm) {
      Object.assign(this.ruleForm, ruleForm);
      this.isEdit = true;
      this.dialogVisible = true;
      this.isResetShow = false
    },
    orgSelect(obj){
        this.ruleForm.orgName = obj.name
    },
    handleClose() {
      this.doReset();
      this.dialogVisible = false;
    },
    doConfirm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            API.edit(this.ruleForm).then((res) => {
              if (res.data.code === 0) {
                this.$message.success("设备信息修改成功!");
                this.handleClose();
                this.$emit("table-search");
              }
            });
          } else {
            PhotoApi.save({ path: this.filePath }).then((res) => {
              if (res.data.code === 0) {
                this.ruleForm.photoId = res.data.data;
                API.save(this.ruleForm).then((res) => {
                  if (res.data.code === 0) {
                    this.$message.success("新增设备成功!");
                    this.handleClose();
                    this.$emit("table-search");
                  }
                });
              }
            });
          }
        }
      });
    },
    doReset() {
      this.$refs["ruleForm"].resetFields();
    },
    uploadSuccess(response) {
      this.filePath = response.data;
      if (response.data.indexOf("/static") > -1) {
        this.filePath = response.data.substr(response.data.indexOf("/static"));
      }
    },
    getCoordinates() {
      this.$refs["coordinates"].show();
    },
  },
};
</script>

<style lang="scss" scoped>
.car-owner-add {
  .el-input {
    width: 220px;
  }
}
</style>